package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.GoodsTypeDao;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.GoodsType;
import com.atguigu.jxc.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private GoodsTypeDao goodsTypeDao;
    @Override
    public List<Goods> selectPage(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {
        List<Integer> arrayList = new ArrayList<>();
        arrayList = this.countGoodsTypeId(goodsTypeId, arrayList);
        if (goodsTypeId != null) {
            arrayList.add(goodsTypeId);
        }
        page = (page-1)*rows;
            List<Goods> goods = goodsDao.selectPage(page, rows, codeOrName, arrayList);
            return goods;
    }

    /**
     * 新增或修改
     * @param goods
     * @param goodsId
     * @return
     */
    @Override
    public Boolean savaOrUpdate(Goods goods, String goodsId) {
        if (goodsId!=null){
            goods.setGoodsId(Integer.parseInt(goodsId));
            return goodsDao.update(goods);
        }else {
            //查询所有商品 判断商品编码和名称是否存在
            List<Goods> goodsList = goodsDao.selectAllGoods();
            for (Goods goods1 : goodsList) {
                if (goods1.getGoodsName().equals(goods.getGoodsName()) || goods1.getGoodsCode().equals(goods.getGoodsCode())){
                    return false;
                }
            }
            goods.setInventoryQuantity(0);
            goods.setLastPurchasingPrice(goods.getPurchasingPrice());
            goods.setState(0);
            return goodsDao.insert(goods);
        }

    }

    /**
     * 删除商品
     * @param goodsId
     * @return
     */
    @Override
    public Boolean deleteGoods(Integer goodsId) {
        Goods goods = goodsDao.selectGoodsById(goodsId);
        //判断商品状态,入库、有进货和销售单据的不能删除
        Integer state = goods.getState();
        if (state == 0){
            goodsDao.deleteGoodsById(goodsId);
            return true;
        }
        return false;
    }

    /**
     * 查询没有库存的商品
     * @param page
     * @param rows
     * @param nameOrCode
     * @return
     */
    @Override
    public List<Goods> getNoInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        page = (page-1)*rows;
        return goodsDao.getNoInventoryQuantity(page,rows,nameOrCode,0);
    }

    /**
     * 查询全部有库存的商品
     * @param page
     * @param rows
     * @param nameOrCode
     * @return
     */
    @Override
    public List<Goods> getHasInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        page=(page-1)*rows;
        return goodsDao.getHasInventoryQuantity(page,rows,nameOrCode);
    }

    /**
     * 添加库存、修改数量或成本价
     * @param goodsId
     * @param inventoryQuantity
     * @param purchasingPrice
     */
    @Override
    public Boolean saveStock(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {
        Boolean flag = goodsDao.saveStock(goodsId, inventoryQuantity, purchasingPrice);
        return flag;

    }

    /**
     * 删除库存（要判断商品状态 入库、有进货和销售单据的不能删除）
     * @param goodsId
     * @return
     */
    @Override
    public Boolean deleteStock(Integer goodsId) {
        Goods goods = goodsDao.selectGoodsById(goodsId);
        if (goods.getState()!=0){
            return false;
        }
        return goodsDao.deleteStock(goodsId);
    }

    /**
     * 查询所有 当前库存量 小于 库存下限的商品信息
     * @return
     */
    @Override
    public List<Goods> selectListAlarm() {
        return goodsDao.selectListAlarm();
    }

    //提取的方法：递归根据分类查询商品信息
    private List<Integer> countGoodsTypeId(Integer goodsTypeId, List<Integer> arrayList) {
        List<GoodsType> goodsTypeByPid = goodsTypeDao.getGoodsTypeByPid(goodsTypeId);
        if (goodsTypeByPid.size()>0&& goodsTypeByPid!=null){
            for (GoodsType goodsType : goodsTypeByPid) {
                if (goodsType.getGoodsTypeState()==0){
                    arrayList.add(goodsType.getGoodsTypeId());
                }else{
                    arrayList.add(goodsType.getGoodsTypeId());
                    this.countGoodsTypeId(goodsType.getGoodsTypeId(),arrayList);
                }
            }
        }
        return arrayList;
    }
}
